package dao

import (
	"gorm.io/gorm"
	"kf/models/entity"
	"time"
)

// 客服分配规则
type SysRuleDao struct {
	DB *gorm.DB
}

// 保存用户角色
func (SysRuleDao *SysRuleDao) SaveSysRule(rule entity.SysRule) error {
	rule.CreateTime = time.Now()
	err := SysRuleDao.DB.Model(&entity.SysRule{}).Create(&rule).Error
	if err != nil {
		return err
	}
	return nil
}

// 删除
func (SysRuleDao *SysRuleDao) DeleteSysRule(rule entity.SysRule) error {
	err := SysRuleDao.DB.Delete(&entity.SysRule{}, "id = ?", rule.ID).Error
	if err != nil {
		return err
	}
	return nil
}

func (SysRuleDao *SysRuleDao) UpdateSysRule(rule entity.SysRuleForm) error {
	err := SysRuleDao.DB.Model(&entity.SysRule{}).
		Where("id = ?", rule.ID).
		Updates(&rule).Error
	if err != nil {
		return err
	}
	return nil
}

// 根据id查询
func (SysRuleDao *SysRuleDao) GetSysRule(rule entity.SysRule) (entity.SysRuleVo, error) {
	var sysRuleBo entity.SysRuleBo
	err := SysRuleDao.DB.Model(&entity.SysRule{}).
		Select("sys_rule.*,sys_user.username").
		Joins("left join sys_user on  sys_rule.kefu_id = sys_user.id").
		Where("sys_rule.id = ?", rule.ID).
		Find(&sysRuleBo).Error
	var vo entity.SysRuleVo
	if err != nil {
		return vo, err
	}
	vo = convertEntity(sysRuleBo)
	return vo, nil
}

// count
func (SysRuleDao *SysRuleDao) GetSysRuleCount(host *string, ip *string) (int64, error) {
	var count int64
	tx := SysRuleDao.DB.Model(&entity.SysRule{})
	if host != nil {
		tx.Where("host like ?", "%"+*host+"%")
	}
	if ip != nil {
		tx.Where("ip like ?", "%"+*ip+"%")
	}
	err := tx.Count(&count).Error
	if err != nil {
		return count, err
	}
	return count, nil
}

// 分页
func (SysRuleDao *SysRuleDao) GetSysRulePage(page entity.RulePageQuery) ([]entity.SysRuleVo, error) {
	pageNum := *page.PageQuery.PageNum
	pageSize := *page.PageQuery.PageSize

	offset := (pageNum - 1) * pageSize
	if offset < 0 {
		offset = 0
	}

	var sysRuleBos []entity.SysRuleBo
	tx := SysRuleDao.DB.Model(&entity.SysRule{})
	if page.Host != nil && *page.Host != "" {
		tx.Where("host like ?", "%"+*page.Host+"%")
	}
	if page.Ip != nil && *page.Ip != "" {
		tx.Where("ip like ?", "%"+*page.Ip+"%")
	}
	err := tx.Select("sys_rule.*,sys_user.username").
		Joins("left join sys_user on  sys_rule.kefu_id = sys_user.id").
		Offset(int(offset)).Limit(int(pageSize)).
		Find(&sysRuleBos).Error
	var vos []entity.SysRuleVo
	for _, bo := range sysRuleBos {
		vo := convertEntity(bo)
		vos = append(vos, vo)
	}
	if err != nil {
		return vos, err
	}
	return vos, nil
}

// 根据域名和ip进行查询
func (SysRuleDao *SysRuleDao) GetSysRuleByHostIp(host string, ip string) []string {
	tx := SysRuleDao.DB.Model(&entity.SysRule{}).Select("kefu_id")
	if host != "" {
		tx.Where("host_use = 1 and ((host_match = 0 and host = ?) or (host_match = 1 and host like ?))", host, "%"+host+"%")
	}
	if ip != "" {
		tx.Or("ip_use = 1 and ((ip_match = 0 and ip = ?) or (ip_match = 1 and ip like ?))", ip, "%"+ip+"%")
	}
	var kefuIds []string
	tx.Order("sort asc").Find(&kefuIds)
	return kefuIds
}

func convertEntity(sysRuleBo entity.SysRuleBo) entity.SysRuleVo {
	vo := entity.SysRuleVo{
		ID:         sysRuleBo.ID,
		KefuId:     sysRuleBo.KefuId,
		KefuName:   sysRuleBo.KefuName,
		Host:       sysRuleBo.Host,
		HostMatch:  sysRuleBo.HostMatch,
		HostUse:    sysRuleBo.HostUse,
		Ip:         sysRuleBo.Ip,
		IpMatch:    sysRuleBo.IpMatch,
		IpUse:      sysRuleBo.IpUse,
		Sort:       sysRuleBo.Sort,
		CreateTime: sysRuleBo.CreateTime.Format("2006-01-02 15:04:05"),
		CreateBy:   sysRuleBo.CreateBy,
	}
	return vo
}
